package com.example.finger_android_code;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.Helper.FingerHelper;
import com.example.Util.CloseResult;
import com.example.Util.DevComm;
import com.example.Util.FingerCallResult;
import com.example.Util.FingerCallback;
import com.example.Util.LogAndToastUtils;

import java.util.Arrays;


public class MainActivity extends AppCompatActivity {
    private FingerHelper fingerHelper;
    private Button open;
    private Button close;
    private Button enRoll;
    private Button openDoor;
    private Button punchCard;
    private Button delete;
    private Button cancel;
    private Button replace;
    private Button deleteAll;
    private Button fingerState;
    private Button jump;
    private Button fingerData;

    private Button inputTemplate;
    private EditText editUserId;
    private TextView state;
    private TextView fingerImageData;
    private byte[] temperData = new byte[DevComm.SZ_TEMPLATE_SIZE];

    private final boolean m_bForce = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setWeight();
        setButListen();

        fingerHelper = new FingerHelper(new FingerCallback() {
            @Override
            public void onResult(FingerCallResult result) {
                ProcessingResults(result);
            }

            @Override
            public void imageDataResult(byte[] resultData) {
                System.arraycopy(resultData,0,temperData,0,DevComm.SZ_TEMPLATE_SIZE);
                MainActivity.this.fingerImageData.setText(Arrays.toString(temperData));
                setState(true);
            }
        },this);
        setState(false);
    }

    private void ProcessingResults(FingerCallResult result) {
        state.setText(result.getMessage());
        setState(true);
    }

    public void setState(boolean isOpen) {
        open.setEnabled(!isOpen);
        close.setEnabled(isOpen);
        enRoll.setEnabled(isOpen);
        openDoor.setEnabled(isOpen);
        punchCard.setEnabled(isOpen);
        delete.setEnabled(isOpen);
        replace.setEnabled(isOpen);
        deleteAll.setEnabled(isOpen);
        fingerState.setEnabled(isOpen);
        cancel.setEnabled(isOpen);
        fingerData.setEnabled(isOpen);
        inputTemplate.setEnabled(isOpen);
    }

    private void setButListen() {
        ButOnclickListenImp butOnclickListenImp = new ButOnclickListenImp();
        open.setOnClickListener(butOnclickListenImp);
        close.setOnClickListener(butOnclickListenImp);
        enRoll.setOnClickListener(butOnclickListenImp);
        openDoor.setOnClickListener(butOnclickListenImp);
        punchCard.setOnClickListener(butOnclickListenImp);
        delete.setOnClickListener(butOnclickListenImp);
        cancel.setOnClickListener(butOnclickListenImp);
        replace.setOnClickListener(butOnclickListenImp);
        fingerState.setOnClickListener(butOnclickListenImp);
        deleteAll.setOnClickListener(butOnclickListenImp);
        jump.setOnClickListener(butOnclickListenImp);
        fingerData.setOnClickListener(butOnclickListenImp);
        inputTemplate.setOnClickListener(butOnclickListenImp);
    }

    private void setWeight() {
        open = findViewById(R.id.but_open);
        close = findViewById(R.id.but_close);
        enRoll = findViewById(R.id.but_enRoll);
        openDoor = findViewById(R.id.but_openDoor);
        punchCard = findViewById(R.id.but_punchCard);
        delete = findViewById(R.id.but_delete);
        cancel = findViewById(R.id.but_cancel);
        replace = findViewById(R.id.but_replace);
        deleteAll = findViewById(R.id.but_deleteAll);
        editUserId = findViewById(R.id.edit_userId);
        fingerState = findViewById(R.id.but_fingerState);
        state = findViewById(R.id.text_state1);
        jump = findViewById(R.id.but_jump);
        fingerImageData = findViewById(R.id.text_image_data_1);
        fingerData = findViewById(R.id.but_imageData);
        inputTemplate = findViewById(R.id.but_input);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class ButOnclickListenImp implements View.OnClickListener {

        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.but_open: {
                    ButOpen();
                    break;
                }
                case R.id.but_close: {
                    ButClose();
                    break;
                }

                case R.id.but_enRoll: {
                    ButEnRoll();
                    break;
                }

                case R.id.but_openDoor: {
                    IdentifyFinger();
                    break;
                }

                case R.id.but_punchCard: {
                    ButPunchCard();
                    break;
                }

                case R.id.but_delete: {
                    ButDelete();
                    break;
                }

                case R.id.but_cancel: {
                    fingerHelper.cancel();
                    break;
                }

                case R.id.but_replace: {
                    ButReplace();
                    break;
                }

                case R.id.but_deleteAll: {
                    ButDeleteAll();
                    break;
                }

                case R.id.but_fingerState: {
                    ButFingerState();
                    break;
                }
                case R.id.but_jump:
                    ButJump();
                    break;
                case R.id.but_imageData:
                    butGetFingerData();
                    break;
                case R.id.but_input:
                    butInputTemplate();
            }
        }


    }

    private void butInputTemplate() {
        if (editUserId.getText() != null && !editUserId.getText().toString().isEmpty()) {
            fingerHelper.inputTemplate(Integer.parseInt(editUserId.getText().toString()),temperData);
        } else {
            enRoll.setEnabled(true);
            Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
        }
        //fingerHelper.inputTemplate(10,temperData);
    }

    private void butGetFingerData() {
        fingerData.setEnabled(false);
        fingerHelper.getFingerTemplate();
    }

    private void ButJump() {
        Intent intent = new Intent(this,MainActivity2.class);
        startActivity(intent);

    }

    private void ButFingerState() {
        fingerState.setEnabled(false);
        //fingerHelper.FingerState(m_bForce);
    }

    private void ButDeleteAll() {
        deleteAll.setEnabled(false);
        fingerHelper.deleteAllFinger(m_bForce);
    }

    private void ButReplace() {
        replace.setEnabled(false);
        if (editUserId.getText() != null && !editUserId.getText().toString().isEmpty()) {
            //fingerHelper.ReplaceTemplate(Integer.parseInt(editUserId.getText().toString()), m_bForce);
        } else {
            replace.setEnabled(true);
            Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
        }
    }

    private void ButDelete() {
        delete.setEnabled(false);
        if (editUserId.getText() != null && !editUserId.getText().toString().isEmpty()) {
            //fingerHelper.DeleteTemplate(Integer.parseInt(editUserId.getText().toString()), m_bForce);
        } else {
            delete.setEnabled(true);
            //Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
        }
    }

    private void ButPunchCard() {
        punchCard.setEnabled(false);
        if (editUserId.getText() != null && !editUserId.getText().toString().isEmpty()) {
            //fingerHelper.PunchCardTemplate(Integer.parseInt(editUserId.getText().toString()), m_bForce);
        } else {
            punchCard.setEnabled(true);
            //Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
        }
    }

    private void IdentifyFinger() {
        openDoor.setEnabled(false);
        //fingerHelper.IdentifyInAllTemplate(m_bForce);
        fingerHelper.identifyFinger(false);
    }

    //录入一个指纹
    private void ButEnRoll() {
        enRoll.setEnabled(false);
        if (editUserId.getText() != null && !editUserId.getText().toString().isEmpty()) {
            fingerHelper.enrollFinger(Integer.parseInt(editUserId.getText().toString()), m_bForce);
        } else {
            enRoll.setEnabled(true);
            Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
        }

    }

    //关闭设备
    private void ButClose() {
        close.setEnabled(false);
        CloseResult closeResult = fingerHelper.closeDevice();
        if (closeResult.getResult()) {
            LogAndToastUtils.log("关闭成功");
            setState(false);
        } else {
            LogAndToastUtils.toast(this,closeResult.getMessage());
            setState(true);
        }

    }

    //打开设备
    private void ButOpen() {
        enRoll.setEnabled(false);
        boolean isOpen = fingerHelper.openDevice(this);
        if(isOpen){
            LogAndToastUtils.log("打开设备成功");
            setState(true);
        }else{
            LogAndToastUtils.log("打开设备失败");
            setState(false);
        }

    }


}